package it.koutifaris.travianmap.dal.sqlservices.deleteservice;

import it.koutifaris.travianmap.context.HasApplicationContex;
import it.koutifaris.travianmap.dal.DeleteService;
import it.koutifaris.travianmap.dal.TravianServerService;
import it.koutifaris.travianmap.dal.TravianServerService.TravianServer;
import it.koutifaris.travianmap.dal.TravianServerServiceFactory;
import it.koutifaris.travianmap.dal.TravianServerSettingsService;

public class SqlDeleteService extends HasApplicationContex implements DeleteService {

	@Override
	public void dropAll() {
		TravianServerServiceFactory serverServiceFactory = getTravianMapContext().getServerServiceFactory();
		dropAllServers(serverServiceFactory);
		dropAllSettings(serverServiceFactory);
	}

	private void dropAllSettings(TravianServerServiceFactory serverServiceFactory) {
		TravianServerSettingsService settingsService = serverServiceFactory.travianServerSettingsService();
		settingsService.deleteAllSettings();

		settingsService.close();
	}

	private void dropAllServers(TravianServerServiceFactory serverServiceFactory) {
		TravianServerService serverService = serverServiceFactory.travianServerService();
		serverService.dropAllServers(getActivity());

		serverService.close();
	}

	@Override
	public void deleteServer(TravianServer travianServer) {
		TravianServerServiceFactory serverServiceFactory = getTravianMapContext().getServerServiceFactory();
		deleteSettings(travianServer, serverServiceFactory);
		deleteServer(travianServer, serverServiceFactory);
	}

	private void deleteSettings(TravianServer travianServer, TravianServerServiceFactory serverServiceFactory) {
		TravianServerSettingsService settingsService = serverServiceFactory.travianServerSettingsService();
		settingsService.deleteSettings(travianServer);
		settingsService.close();
	}

	private void deleteServer(TravianServer travianServer, TravianServerServiceFactory serverServiceFactory) {
		TravianServerService travianServerService = serverServiceFactory.travianServerService();
		travianServerService.dropServer(getActivity(), travianServer);
		travianServerService.close();
	}

}
